import com.meirengu.utils.NumberUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class read {

    /**
     * tomcat
     * @param args
     */
    /*public static void main(String[] args){
        File file=new File("/Users/lx/Desktop/access.log-20180815");
        BufferedReader reader=null;
        File file2=new File("/Users/lx/Desktop/access2.log");
        String temp=null;
        int line=1;
        Set set = new HashSet();
        try{
            reader=new BufferedReader(new FileReader(file));
            while((temp=reader.readLine())!=null){
                if (temp.contains("CheckCodeController.create params >> mobile")) {
                    //System.out.println("line"+line+":"+temp);
                    String mobile = temp.substring(temp.length()-11,temp.length());
                    System.out.println(mobile);
                    set.add(mobile);
                    line++;
                }

            }
            *//*reader=new BufferedReader(new FileReader(file2));
            while((temp=reader.readLine())!=null){
                if (temp.contains("CheckCodeController.create params >> mobile")) {
                    //System.out.println("line"+line+":"+temp);
                    String mobile = temp.substring(temp.length()-11,temp.length());
                    set.add(mobile);
                    System.out.println(mobile);
                    line++;
                }

            }*//*
     *//*Iterator it = set.iterator();
            while (it.hasNext()) {
                System.out.println(it.next());
            }*//*

        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            if(reader!=null){
                try{
                    reader.close();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }*/

    /**
     * nginx
     *
     * @param args
     */
    public static void main(String[] args) {
        File file = new File("/Users/lx/Desktop/access.log-20180815");
        BufferedReader reader = null;
        File file2 = new File("/Users/lx/Desktop/access2.log");
        String temp = null;
        int line = 1;
        Set<String> set = new HashSet<String>();
        Set<String> lineSet = new HashSet<String>();
        Map<String, Integer> map = new HashMap<>();
        try {
            reader = new BufferedReader(new FileReader(file));
            while ((temp = reader.readLine()) != null) {
                if (temp.contains("/webview/vitify_code") && temp.contains("404")) {
                    System.out.println("line" + line + ":" + temp);
                    String ip = temp.split(" - - ")[0];
                    System.out.println(temp.split(" - - ")[0]);
                    String[] arr = temp.split("\"");
                    System.out.println(arr[3].split("=")[1]);
                    String channel = arr[3].split("=")[1];
                    if (!set.contains(ip)) {
                        set.add(ip);
                        Integer num = map.get(channel);
                        if (NumberUtil.isNullOrZero(num)) {
                            map.put(channel, 1);
                        } else {
                            map.put(channel, num + 1);
                        }
                        lineSet.add(temp);
                    } else {
                        System.out.println("重复");
                    }

                    line++;
                }

            }
            reader = new BufferedReader(new FileReader(file2));
            while ((temp = reader.readLine()) != null) {
                if (temp.contains("/webview/vitify_code") && temp.contains("404")) {
                    System.out.println("line" + line + ":" + temp);
                    String ip = temp.split(" - - ")[0];
                    System.out.println(temp.split(" - - ")[0]);
                    String[] arr = temp.split("\"");
                    System.out.println(arr[3].split("=")[1]);
                    String channel = arr[3].split("=")[1];
                    if (!set.contains(ip)) {
                        set.add(ip);
                        Integer num = map.get(channel);
                        if (NumberUtil.isNullOrZero(num)) {
                            map.put(channel, 1);
                        } else {
                            map.put(channel, num + 1);
                        }
                        lineSet.add(temp);
                    } else {
                        System.out.println("重复");
                    }

                    line++;
                }

            }
            /*Iterator it = lineSet.iterator();
            while (it.hasNext()) {
                System.out.println(it.next());
            }*/
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
